package com.example.coupon.job;

import com.example.coupon.comon.enums.CouponStatusEnum;
import com.example.coupon.dao.CouponMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class CouponJob {

    @Autowired
    private CouponMapper couponMapper;

    @XxlJob("startCouponHandler")
    public void startCouponHandler() throws Exception {
        //1、查询出来需要执行开始的优惠券
        List<Map<String, Object>> couponList = couponMapper.queryCanStartCoupon(CouponStatusEnum.NOT_START.getCode());
        if (couponList.size() > 0 ){
            couponList.forEach(couponMap -> {
                Long id = Long.parseLong(couponMap.get("id").toString());
                System.out.println("开始优惠券，id = " + id);
                couponMapper.updateStatusById(CouponStatusEnum.UNDERWAY.getCode(), id);
            });
        }
    }

    @XxlJob("expireCouponHandler")
    public void expireCouponHandler() throws Exception {
        //1、查询出来需要执行开始的优惠券
        List<Map<String, Object>> couponList = couponMapper.queryCanExpireCoupon(CouponStatusEnum.UNDERWAY.getCode());
        if (couponList.size() > 0 ){
            couponList.forEach(couponMap -> {
                Long id = Long.parseLong(couponMap.get("id").toString());
                System.out.println("过期优惠券，id = " + id);
                couponMapper.updateStatusById(CouponStatusEnum.EXPIRE.getCode(), id);
            });
        }
    }
}